//
//  JMPhoneCodeTool.m
//  XPBiPro
//
//  Created by liuny on 2018/5/17.
//  Copyright © 2018年 liuny. All rights reserved.
//
///register 注册 forget_password 忘记密码 transfer 转账 set_transfer_password 设置交易密码
#import "JMPhoneCodeTool.h"

@interface JMPhoneCodeTool()
{
    NSTimer *_timer;
    //重发间隔
    NSInteger _secondsSendAgain;
    SendPhoneCodeType _type;
}
@property(nonatomic, strong) UIButton *phoneCodeButton;
@end

@implementation JMPhoneCodeTool
-(instancetype)initWithPhoneCodeButton:(UIButton *)phoneCodeButton
                                  type:(SendPhoneCodeType)type{
    self = [super init];
    if(self){
        self.phoneCodeButton = phoneCodeButton;
        _type = type;
    }
    return self;
}

-(void)initControl{
    //初始化定时器，使用timerWithTimeInterval此种方法初始化不会立刻触发
    _secondsSendAgain = 60;
    if (!_timer) {
        kWeakSelf(self);
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 block:^(NSTimer * _Nonnull timer) {
            [weakself timerAction];
        } repeats:YES];
    }
}
-(void)timerAction{
    _secondsSendAgain--;
    DLog(@"倒计时%ld",_secondsSendAgain);
    self.phoneCodeButton.enabled = _secondsSendAgain == 0;
    NSString *title = [NSString stringWithFormat:@"重新获取(%ld)",(long)_secondsSendAgain];
    self.phoneCodeButton.titleLabel.text = title;
    [self.phoneCodeButton setTitle:title forState:UIControlStateNormal];
    if(_secondsSendAgain == 0){
        [self stopTimer];
        _secondsSendAgain = 60;
        [self.phoneCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    }
}

//停止计时
-(void)stopTimer{
    [_timer invalidate];
    _timer = nil;
}

//开始计时
-(void)startTimer{
    [self initControl];
}

/**
 发送验证码
 */
-(void)sendPhoneCode{
            //网络请求发送验证码
            if(self.phoneNum.length == 0){
                return;
            }
            //验证手机的格式
            BOOL isMoblie = [RegularExp isMobileGuoJi:self.phoneNum];
            if(isMoblie == NO){
                return;
            }
            [self sendCodeForRegisterAndPassword];
}

/**
 注册跟密码操作发送验证码
 */
-(void)sendCodeForRegisterAndPassword{
    NSString *action;
    switch (_type) {
        case SendPhoneCodeType_Password:
            action = @"forget_password";
            break;
        case SendPhoneCodeType_Register:
            action = @"register";
            break;
        case SendPhoneCodeType_Transfer:
            action = @"transfer";
            break;
        case SendPhoneCodeType_Set_transfer_password:
            action = @"set_transfer_password";
            break;
        default:
            break;
    }
    NSMutableDictionary *param = @{}.mutableCopy;
    param[@"mobile"] = self.phoneNum;
    param[@"action"] = action;
    [MBProgressHUD showLoadTitile:@"" ToView:[kAppDelegate getCurrentVC].view];
//    kWeakSelf(self);
//    [NetworkManager POST:URL_user_sms parameters:param success:^(id responseObject) {
//       
//        [MBProgressHUD hideHUDForView:[kAppDelegate getCurrentVC].view];
//        if ([responseObject[@"code"] integerValue]==kSuccessCode) {
//            if (!ProductSever) {
//                [[kAppDelegate getCurrentVC] AlertWithTitle:[NSString stringWithFormat:@"%ld",[responseObject[@"data"]integerValue]] message:@"" andOthers:@[@"确定"] animated:YES action:^(NSInteger index) {
//                    
//                }];
//            }
//            [weakself startTimer];
//        }else{
//            if (weakself.ErrorBlock) {
//                weakself.ErrorBlock(responseObject[@"message"]);
//            }else{
//                [MBProgressHUD showWarn:responseObject[@"message"] ToView:[kAppDelegate getCurrentVC].view];
//            }
//        }
//    } failure:^(NSError *error) {
//        if (weakself.ErrorBlock) {
//            weakself.ErrorBlock(kNetError);
//        }else{
//            [MBProgressHUD showWarn:kNetError ToView:[kAppDelegate getCurrentVC].view];
//        }
//    }];
}
- (void)dealloc{
    [self stopTimer];
    DLog(@"定时器走了");
}

@end
